/*****************************************************
 * Copyright ?, 2006-2008 Huawei Technologies Co.,Ltd. 
 * Filename: halcaps.cpp
 * Version: 1.0 / Author: Lei Yu / Date: 2007-4-27
 * Description: 
 * Version: 
 * Function List :
 * History:
 * leiyu    2007-4-27   1.0  create this file
 ****************************************************/
 
#include "precomp.h"

DWORD gFourCC[] = {
    FOURCC_YV12,
    FOURCC_NV12,        
    FOURCC_YUY2,
    FOURCC_YVYU,
    FOURCC_UYVY
};
#define MAX_FOURCC    (sizeof(gFourCC)/sizeof(DWORD))

/* callbacks from the DIRECTDRAW object */
DDHAL_DDCALLBACKS cbDDCallbacks =
{
    sizeof(DDHAL_DDCALLBACKS),    // dwSize
    DDHAL_CB32_CREATESURFACE |          // dwFlags
    DDHAL_CB32_WAITFORVERTICALBLANK |
    DDHAL_CB32_CANCREATESURFACE |
    DDHAL_CB32_GETSCANLINE |
    0,
    HalCreateSurface,            // CreateSurface
    HalWaitForVerticalBlank,        // WaitForVerticalBlank
    HalCanCreateSurface,            // CanCreateSurface
    0,                    // CreatePalette
    HalGetScanLine                 // GetScanLine
};

/* callbacks from the DIRECTDRAWMISCELLANEOUS object */

DDHAL_DDMISCELLANEOUSCALLBACKS MiscellaneousCallbacks =
{
    sizeof(DDHAL_DDMISCELLANEOUSCALLBACKS),
//  DDHAL_MISCCB32_GETAVAILDRIVERMEMORY |
//  DDHAL_MISCCB32_GETDEVICEIDENTIFIER
    0,
    0, //HalGetAvailDriverMemory,
    0  //HalGetDeviceIdentifier
};

/* callbacks from the DIRECTDRAWPALETTE object */

DDHAL_DDPALETTECALLBACKS cbDDPaletteCallbacks =
{
    sizeof( DDHAL_DDPALETTECALLBACKS ),   // dwSize
//  DDHAL_PALCB32_DESTROYPALETTE |        // dwFlags
//  DDHAL_PALCB32_SETENTRIES |
    0,
    0,                  // DestroyPalette
    0                   // SetEntries
};

// callbacks from the DIRECTDRAWSURFACE object

DDHAL_DDSURFACECALLBACKS cbDDSurfaceCallbacks =
{
    sizeof( DDHAL_DDSURFACECALLBACKS ), // dwSize
    DDHAL_SURFCB32_DESTROYSURFACE |    // dwFlags
    DDHAL_SURFCB32_FLIP |
    DDHAL_SURFCB32_LOCK |
    DDHAL_SURFCB32_UNLOCK |
    DDHAL_SURFCB32_SETCOLORKEY |
    DDHAL_SURFCB32_GETBLTSTATUS |
    DDHAL_SURFCB32_GETFLIPSTATUS |
    DDHAL_SURFCB32_UPDATEOVERLAY |
    DDHAL_SURFCB32_SETOVERLAYPOSITION |
    DDHAL_SURFCB32_SETPALETTE |
    0,
    HalDestroySurface,            // DestroySurface
    HalFlip,            // Flip
    DDGPELock,            // Lock
    DDGPEUnlock,            // Unlock
    HalSetColorKey,            // SetColorKey
    HalGetBltStatus,        // GetBltStatus
    HalGetFlipStatus,        // GetFlipStatus
    HalUpdateOverlay,        // UpdateOverlay
    HalSetOverlayPosition,        // SetOverlayPosition
    0,                // SetPalette
};

//#define SCREEN_WIDTH  (GetDDGPE()->ScreenWidth())
//#define SCREEN_HEIGHT   (GetDDGPE()->ScreenHeight())

/* InitDDHALInfo must set up this information */
unsigned long   g_nVideoMemorySize    = 0L;
unsigned char * g_pVideoMemory        = NULL; // virtual address of video memory from client's side
DWORD        g_nTransparentColor    = 0L;

/*****************************************************
 * Function: buildDDHALInfo
 * Description: sets up the global variables that 
 *              DDGPE requires.
 * Calls: 
 * Called by: 
 * Table Access: list the table you will use. 
 * Table Update: list the table which you will update.
 * Input: lpddhi - A pointer to a DDHALINFO structure 
 *                 that should be populated with the 
 *                 appropriate settings.
 *        modeIndex - A DWORD indicating the current mode.
 * Output: NONE
 * Return: NONE
 * Others: NONE
 ****************************************************/
EXTERN_C void buildDDHALInfo(LPDDHALINFO lpddhi, DWORD)
{
    int  i;
    /* Clear the DDHALINFO structure */
    DEBUGMSG(GPE_ZONE_HISI, (TEXT("DirectDraw: buildDDHALInfo.\r\n")));

    if (NULL == lpddhi)
    {
        return;
    }
    
    (void)memset(lpddhi, 0, sizeof(DDHALINFO));

    if (!g_pVideoMemory)    // in case this is called more than once...
    {
        unsigned long physicalVideoMemoryStart;
        GetDDGPE()->GetPhysicalVideoMemory(&physicalVideoMemoryStart, &g_nVideoMemorySize);
        DEBUGMSG(GPE_ZONE_HISI,(TEXT("GetPhysicalVideoMemory returned phys=0x%08x size=%d\r\n"),
            physicalVideoMemoryStart, g_nVideoMemorySize));

        g_pVideoMemory = (BYTE*)physicalVideoMemoryStart;
        DEBUGMSG(GPE_ZONE_HISI,(TEXT("gpVidMem=%08x\r\n"), g_pVideoMemory));
    }

    /* Populate the DDHALINFO structure: */

    lpddhi->dwSize = sizeof(DDHALINFO);
    lpddhi->lpDDCallbacks = &cbDDCallbacks;
    lpddhi->lpDDSurfaceCallbacks = &cbDDSurfaceCallbacks;
    lpddhi->lpDDPaletteCallbacks = &cbDDPaletteCallbacks;

    lpddhi->GetDriverInfo = HalGetDriverInfo;

    lpddhi->lpdwFourCC = gFourCC;        // fourcc codes supported

    /* hw specific caps: */
    lpddhi->ddCaps.dwSize = sizeof(DDCAPS);            // size of the DDCAPS structure
    lpddhi->ddCaps.dwVidMemTotal = g_nVideoMemorySize;  // total amount of video memory
    lpddhi->ddCaps.dwVidMemFree = g_nVideoMemorySize;   // amount of free video memory
    lpddhi->ddCaps.ddsCaps.dwCaps =            // DDSCAPS structure has all the general capabilities
    DDSCAPS_BACKBUFFER |            // Can create backbuffer surfaces
    DDSCAPS_FLIP |                // Can flip between surfaces
    DDSCAPS_FRONTBUFFER |            // Can create front-buffer surfaces
    DDSCAPS_OVERLAY |                // Can create overlay surfaces
    DDSCAPS_PRIMARYSURFACE |            // Has a primary surface
    DDSCAPS_SYSTEMMEMORY |                    // Surfaces are in system memory
    DDSCAPS_VIDEOMEMORY |            // Surfaces are in video memory
    0;
    lpddhi->ddCaps.dwNumFourCCCodes = MAX_FOURCC;   // number of four cc codes

    lpddhi->ddCaps.dwPalCaps =  0;         // palette capabilities
    

    lpddhi->ddCaps.dwBltCaps =  0;          // special hardware blitting capabilities
        

    lpddhi->ddCaps.dwCKeyCaps =                // color key hardware blitting
        DDCKEYCAPS_SRCBLT |                        // Supports transparent blitting using the color key for the source with this surface for RGB colors.  
        0;

    lpddhi->ddCaps.dwAlphaCaps =                    // alpha hardware blitting
        DDALPHACAPS_VALID |            // Supports per-pixel alpha values specified alongside with the RGB values in the pixel structure.
        0;
    
    for(i = 0; i < DD_ROP_SPACE; i ++)
    {
        lpddhi->ddCaps.dwRops[i] = 0xffffffff;
    }

    //SETROPBIT(lpddhi->ddCaps.dwRops, SRCCOPY);      // Set bits for ROPS supported
    //SETROPBIT(lpddhi->ddCaps.dwRops, PATCOPY);
    //SETROPBIT(lpddhi->ddCaps.dwRops, BLACKNESS);    /*lint !e778 !e572*/
    //SETROPBIT(lpddhi->ddCaps.dwRops, WHITENESS);    /*lint !e648*/

    lpddhi->ddCaps.dwOverlayCaps =                  // overlay capabilities
        DDOVERLAYCAPS_FLIP |                        // Overlay may be flipped.
        DDOVERLAYCAPS_CKEYDESTCLRSPACE |        // Supports overlaying with color keying space of the replaceable bits of the destination surface being overlaid for RGB colors.
        DDOVERLAYCAPS_CKEYSRCCLRSPACE |             // Supports overlaying with color keying space of the replaceable bits of the src surface being overlaid for RGB colors.
        DDOVERLAYCAPS_CKEYDEST |                // Supports overlaying with color keying of the replaceable bits of the destination surface being overlaid for RGB colors.
        DDOVERLAYCAPS_CKEYSRC |                    // Supports overlaying with color keying of the replaceable bits of the src surface being overlaid for RGB colors.
        DDOVERLAYCAPS_OVERLAYSUPPORT |            // Supports overlay surfaces.
        DDOVERLAYCAPS_FOURCC |
        DDOVERLAYCAPS_CKEYBOTH |
        0;

    lpddhi->ddCaps.dwMaxVisibleOverlays = 1;        // maximum number of visible overlays
    lpddhi->ddCaps.dwCurrVisibleOverlays = 0;       // current number of visible overlays
    lpddhi->ddCaps.dwMinOverlayStretch = g_bPSurfinESI ? 250 : 750;        // Min Overlay Stretch factor
    lpddhi->ddCaps.dwMaxOverlayStretch= (320 / 32) * 1000/*1000*/; // Max Overlay Stretch factor
    lpddhi->ddCaps.dwAlignBoundarySrc = 8;        // overlay source rectangle alignment
    lpddhi->ddCaps.dwAlignSizeSrc = 8;            // overlay source rectangle byte size
    lpddhi->ddCaps.dwAlignBoundaryDest = 0;        // overlay destination rectangle alignment
    lpddhi->ddCaps.dwAlignSizeDest = 8;            // overlay destination rectangle byte size
    lpddhi->ddCaps.dwMiscCaps =                     // more driver specific capabilities
        DDMISCCAPS_READSCANLINE |                   // The driver allows the current scanline number to be read
        DDMISCCAPS_READVBLANKSTATUS |               // The driver allows the current vertical blank status to be read
        DDMISCCAPS_FLIPVSYNCWITHVBI |               // The driver is able to synchronize flips with the vertical blanking period
        0;

    /*
    lpddhi->ddCaps.dwMaxVideoPorts  = 1;
    lpddhi->ddCaps.dwCurrVideoPorts = 0;
    */
}
